package com.rc.saas.tenant.controller.app;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.banner.TenantBannerController;
import com.rc.saas.tenant.model.app.TenantAppUpgrade;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.app.TenantAppInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author frog
 * @date 2021/4/15
 */
@Controller
@RequestMapping("/tenantAppInfo")
public class TenantAppInfoController {
    private static Logger logger = LogManager.getLogger(TenantBannerController.class);


    @Autowired
    private TenantAppInfoService tenantAppInfoService;


    /**
     * 跳转APP发版页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/app/tenantAppInfo";
    }


    /**
     * 返回APP发版列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantAppInfo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantAppInfo(BaseSearch baseSearch) {
        PageInfo<TenantAppUpgrade> pageInfo = tenantAppInfoService.listPageTenantAppInfo(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加APP发版页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantAppInfo", method = RequestMethod.GET)
    public String addTenantAppInfo() {
        return "/templates/app/addTenantAppInfo";
    }


    /**
     * 保存/编辑APP发版信息
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantAppInfo", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantInvoiceInfo(TenantAppUpgrade tenantAppUpgrade) {
        if (!BeanUtil.isValid(tenantAppUpgrade.getUpgradeType(), tenantAppUpgrade.getAppVersion(),
                tenantAppUpgrade.getPublishTime(), tenantAppUpgrade.getVersionDesc(), tenantAppUpgrade.getVersionName(),
                tenantAppUpgrade.getAppLocation(), tenantAppUpgrade.getOsMinVersion(), tenantAppUpgrade.getIsMustUpdate())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            tenantAppInfoService.saveTenantInvoiceInfo(tenantAppUpgrade, user.getTenantCode());
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存APP发版失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * APP发版审核通过/驳回
     */
    @ResponseBody
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public TResponse review(Long appUpgradeId, String status) {
        try {
            //status  0:通过 1:驳回
            tenantAppInfoService.saveReview(appUpgradeId, status);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("APP发版失败", e);
            return TResponse.FAIL("APP发版失败");
        }
    }
}
